<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
//引入模型
use App\Http\Models\DemoModel;
use Alert;

class DemoController extends Controller
{
    public function lst()
    {
      $demoModel = new DemoModel();
      $demoData = $demoModel::all();
      return view('demo/lst',['demoData'=>$demoData]);
    }
    //
    public function add()
    {
        if(request()->isMethod('post')){
           $username = request()->input('username');
           $title = request()->input('title');
           $content = request()->input('content');

           $demoModel = new DemoModel();
           $demoModel->username = $username;
           $demoModel->title = $title;
           $demoModel->content = $content;

           $rs = $demoModel->save();
           if($rs){
               return redirect('/admin/demo/lst');
           }else{
               Alert::message('添加失败','提示信息');
               return back();
           }

        }else{
            return view('demo.add');
        }

    }
    //
    public function del($id = 0)
    {

        $demoModel = new DemoModel();

        $demoObj = $demoModel::find($id);

        $rs = $demoObj->delete();
        if($rs){
            return redirect('/admin/demo/lst');
        }else{
            return back();
        }

    }
    //
    public function edt($id = 0)
    {
       if(request()->isMethod('post')){
         $id = request()->input('id');
         $demoData = DemoModel::find($id);

         $username = request()->input('username');
         $title = request()->input('title');
         $content = request()->input('content');

         $demoData->username = $username;
         $demoData->title = $title;
         $demoData->content = $content;

         $rs = $demoData->save();
         if($rs){
             return redirect('/admin/demo/lst');
         }else{
             return back();
         }
       } else{
           $demoData = DemoModel::find($id);
           return view('demo.edt',['demoData'=>$demoData]);
       }

    }
}